package com.hailiao.beidou.message.ui.page.status

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.d10ng.compose.ui.AppColor
import com.d10ng.compose.ui.AppText
import com.d10ng.compose.ui.PageTransitions
import com.d10ng.compose.ui.base.Button
import com.d10ng.compose.ui.base.ButtonSize
import com.d10ng.compose.ui.base.ButtonType
import com.d10ng.compose.ui.base.CellGroup
import com.d10ng.compose.ui.base.CellRow
import com.d10ng.compose.ui.navigation.NavBar
import com.d10ng.compose.ui.show.BDSignalBeamBox
import com.hailiao.beidou.message.utils.back
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.annotation.RootNavGraph

/**
 * 设备状态
 * @Author d10ng
 * @Date 2023/11/17 16:55
 */
@RootNavGraph
@Destination(style = PageTransitions::class)
@Composable
fun StatusScreen(
    model: StatusScreenViewModel = viewModel()
) {
    val deviceStatusList by model.deviceStatusListFlow.collectAsState(listOf())
    val isBDConnected by model.isBDConnectedFlow.collectAsState(false)
    val bdDeviceStatusList by model.bdDeviceStatusListFlow.collectAsState(listOf())
    val bdSignalValue by model.bdSignalValueFlow.collectAsState(listOf())
    StatusScreenView(
        deviceStatusList = deviceStatusList,
        isBDConnected = isBDConnected,
        bdDeviceStatusList = bdDeviceStatusList,
        bdSignalValue = bdSignalValue,
        onClickDisconnect = model::onClickDisconnect
    )
}

@Composable
private fun StatusScreenView(
    deviceStatusList: List<DeviceStatusData> = listOf(),
    isBDConnected: Boolean = false,
    bdDeviceStatusList: List<DeviceStatusData> = listOf(),
    bdSignalValue: List<Pair<Int, Int>> = listOf(),
    onClickDisconnect: () -> Unit = {}
) {
    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(AppColor.Neutral.bg)
            .navigationBarsPadding()
    ) {
        NavBar(
            title = "设备状态",
            titleAlignment = Alignment.CenterStart,
            onClickBack = { back() }
        ) {
            if (isBDConnected) {
                Button(
                    text = "断开连接",
                    type = ButtonType.DANGER,
                    size = ButtonSize.MINI,
                    plain = true,
                    onClick = onClickDisconnect,
                    modifier = Modifier.padding(end = 19.dp)
                )
            }
        }
        Column(
            modifier = Modifier
                .fillMaxSize()
                .weight(1f)
                .verticalScroll(rememberScrollState())
        ) {
            CellGroup(title = "本机设备状态", border = false) {
                deviceStatusList.forEach {
                    StatusCell(value = it)
                }
            }
            if (isBDConnected && bdDeviceStatusList.isNotEmpty()) {
                CellGroup(title = "北斗设备状态", border = false) {
                    bdDeviceStatusList.forEach {
                        StatusCell(value = it)
                    }
                }
            }
            if (isBDConnected) {
                BDSignalBeamBox(beams = bdSignalValue)
            }
        }
    }
}

@Composable
private fun StatusCell(
    value: DeviceStatusData
) {
    CellRow(
        modifier = Modifier.height(62.dp),
        divider = true,
        dividerPaddingValues = PaddingValues(start = 56.dp, end = 19.dp),
        contentPaddingValues = PaddingValues(horizontal = 19.dp)
    ) {
        Image(
            painter = painterResource(id = value.iconId),
            contentDescription = value.title,
            modifier = Modifier.size(25.dp),
            contentScale = ContentScale.Fit
        )
        Text(
            text = value.title,
            style = AppText.Normal.Title.default,
            maxLines = 1,
            overflow = TextOverflow.Ellipsis,
            modifier = Modifier
                .padding(horizontal = 12.dp)
                .weight(1f)
        )
        SelectionContainer {
            Text(
                text = value.value,
                style = AppText.Normal.Title.default,
                maxLines = 1,
                overflow = TextOverflow.Ellipsis,
            )
        }
    }
}

@Preview
@Composable
private fun StatusScreenPreview() {
    StatusScreenView()
}